
#ifndef RTC_DATA_H_
#define RTC_DATA_H_

#include <time.h>

struct RtcTimestamp
{
  RtcTimestamp(void)
  {
    valid = false;
  }

  RtcTimestamp(const RtcTimestamp &timestamp)
  {
    this->rawtime = timestamp.rawtime;
    this->valid = timestamp.valid;
  }

  RtcTimestamp(time_t rawtime)
  {
    this->rawtime = rawtime;
    valid = true;
  }

  RtcTimestamp(int year, int month, int day, int hour, int minute, int second)
  {
    struct tm *time;
    rawtime = 0;
    time = localtime(&rawtime);
    time->tm_year = year - 1900; // Years since 1900
    time->tm_mon = month;
    time->tm_mday = day;
    time->tm_hour = hour;
    time->tm_min = minute;
    time->tm_sec = second;
    time->tm_isdst = 0;
    rawtime = mktime(time);
    valid = true;
  }

  bool isValid(void) const
  {
    return valid;
  }

  time_t getRawtime(void) const
  {
    return rawtime;
  }

  RtcTimestamp& operator= (const RtcTimestamp &timestamp)
  {
    this->rawtime = timestamp.rawtime;
    this->valid = timestamp.valid;
    return *this;
  }

  RtcTimestamp& operator+= (int seconds)
  {
    rawtime += seconds;
  }

  RtcTimestamp& operator+ (int seconds)
  {
    rawtime += seconds;
  }

  int operator- (const RtcTimestamp &timestamp)
  {
    return rawtime - timestamp.rawtime;
  }

  bool operator< (const RtcTimestamp &timestamp)
  {
    this->valid &= timestamp.isValid();
    return rawtime < timestamp.rawtime;
  }

  bool operator<= (const RtcTimestamp &timestamp)
  {
    this->valid &= timestamp.isValid();
    return rawtime <= timestamp.rawtime;
  }

  bool operator> (const RtcTimestamp &timestamp)
  {
    this->valid &= timestamp.isValid();
    return rawtime > timestamp.rawtime;
  }

  bool operator>= (const RtcTimestamp &timestamp)
  {
    this->valid &= timestamp.isValid();
    return rawtime >= timestamp.rawtime;
  }

  private:
    time_t rawtime;
    bool valid;
};

#endif /* RTC_DATA_H_ */
